package hedron.editor;

import static hedron.i18n.Internationalization._;
import hedron.Activator;
import hedron.Application;
import hedron.dialog.ColumnDialog;
import hedron.editorinput.DummyEditorInput;
import hedron.ui.UIFactory;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.EditorPart;

public class InvoiceEditor extends EditorPart {
	public static final String ID = "HEDRON.InvoiceEditor";

	private String invoiceNumber = "0000000005";
	private FormToolkit toolkit;
	private ScrolledForm form;

	Text stockField;
	Combo storesCombo;

	Table stocksTable;
	Table salesTable;

	void refresh() {

	}


	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
		setTitleImage(Activator.getImage("icons/invoice.png"));
		
		((DummyEditorInput) getEditorInput()).setName(_("Editor.Invoice.Title") + ": " + invoiceNumber);

	}

	public void dispose() {
		toolkit.dispose();
		super.dispose();
	}

	@Override
	public void setFocus() {}

	private static final String layout = "<layout columns=\"2\" align=\"FILL\" valign=\"FILL\">"
			+ "<section text=\"Editor.Invoice.Section.Document\" align=\"BEGINING\" valign=\"BEGINING\">"
			+ "     <combo id=\"storesCombo\" readonly=\"true\" align=\"FILL\" valign=\"BEGINING\"/>"
			+ "     <button id=\"documentNumberButton\" width=\"170\" text=\"\"height=\"20\" align=\"FILL\" align=\"BEGINING\"/>"
			+ "</section>"

			+ "<section text=\"Editor.Invoice.Section.Stock\" rowspan=\"2\" columns=\"5\" align=\"FILL\" valign=\"BEGINING\">"
			+ "     <label text=\"Column.Quantity\" align=\"END\" valign=\"CENTER\"/>"
			+ "     <spinner id=\"quantitySpinner\" width=\"70\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "     <label text=\"Column.Stock\" align=\"END\" valign=\"CENTER\"/>"
			+ "     <text id=\"stockField\" align=\"FILL\" valign=\"CENTER\"/>"
			+ "     <button id=\"addStockButton\" text=\"Editor.Invoice.Button.AddStock\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"CENTER\"/>"

			+ "     <label text=\"%\" align=\"END\" valign=\"CENTER\"/>"
			+ "     <spinner id=\"discountSpinner\" width=\"70\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "     <label text=\"Column.Price\" align=\"END\" valign=\"CENTER\"/>"
			+ "     <spinner id=\"priceSpinner\" width=\"70\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "     <label text=\"\" align=\"END\" valign=\"CENTER\"/>"

			+ "     <label text=\"Column.Stocks\" align=\"END\" valign=\"BEGINING\"/>"
			+ "     <table id=\"stocksTable\" colspan=\"3\" fullselection=\"true\" align=\"FILL\" valign=\"FILL\"/>"
			+ "     <label align=\"BEGINING\" valign=\"BEGINING\"/>"
			+ "</section>"

			+ "<section text=\"Column.Customer\" align=\"BEGINING\" valign=\"BEGINING\">"
			+ "     <label id=\"contactNameLabel\" align=\"FILL\" valign=\"BEGINING\"/>"
			+ "     <label id=\"contactIDLabel\" align=\"FILL\" valign=\"BEGINING\"/>"
			+ "     <label id=\"contactVATLabel\" align=\"FILL\" valign=\"BEGINING\"/>"
			+ "     <button id=\"documentNumberButton\" text=\"Editor.Invoice.Button.Contact\" width=\"170\" height=\"20\" align=\"FILL\" align=\"BEGINING\"/>"
			+ "</section>"

			+ "<section text=\"Editor.Invoice.Section.StockList\" colspan=\"2\" columns=\"2\" align=\"FILL\" valign=\"FILL\">"
			+ "     <table id=\"stocksTable\" fullselection=\"true\" align=\"FILL\" valign=\"FILL\"/>"
			+ "     <container align=\"BEGINING\" valign=\"FILL\">"
			+ "         <button id=\"serialButton\" text=\"Editor.Invoice.Button.Serial\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"BEGINNING\"/>"
			+ "         <label align=\"BEGINING\" valign=\"BEGINING\"/>"
			+ "         <button id=\"removeButton\" text=\"Editor.Invoice.Button.Remove\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"BEGINNING\"/>"
			+ "         <button id=\"clearButton\" text=\"Editor.Invoice.Button.Clear\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"BEGINNING\"/>"
			+ "     </container>" 
			+ "</section>" 

			+ "<section text=\"Editor.Invoice.Section.DocumentPrice\" colspan=\"2\" columns=\"2\" align=\"FILL\" valign=\"END\">"
			+ "     <label id=\":\" align=\"END\" valign=\"BEGINING\"/>"
			+ "     <label id=\"\"  align=\"END\" valign=\"BEGINING\"/>"
			+ "     <label id=\":\" align=\"END\" valign=\"BEGINING\"/>"
			+ "     <label id=\"\"  align=\"END\" valign=\"BEGINING\"/>"
			+ "     <label id=\":\" align=\"END\" valign=\"BEGINING\"/>"
			+ "     <label id=\"\"  align=\"END\" valign=\"BEGINING\"/>"
			+ "     <label align=\"FILL\" valign=\"BEGINING\"/>"
			+ "     <button id=\"finalizeButton\" text=\"Editor.Invoice.Button.Finalize\" width=\"170\" height=\"20\" align=\"END\" align=\"BEGINING\"/>"
			+ "</section>"
			+ "</layout>";

	/*
	 * "<layout columns=\"2\" align=\"FILL\" valign=\"FILL\">" +
	 * "<section text=\"Editor.Store.Section.Store\" align=\"BEGINING\" valign=\"BEGINING\">"
	 * +
	 * "     <combo id=\"storesCombo\" width=\"170\" readonly=\"true\" align=\"FILL\" valign=\"BEGINING\"/>"
	 * + "</section>" +
	 * "<section text=\"Editor.Store.Section.Search\" columns=\"3\" align=\"FILL\" valign=\"BEGINING\">"
	 * +
	 * "     <label text=\"Editor.Store.Label.Search\" align=\"BEGINING\" valign=\"CENTER\"/>"
	 * + "      <text  id=\"filterField\" align=\"FILL\" valign=\"CENTER\"/>" +
	 * "      <button id=\"searchButton\" text=\"Editor.Store.Button.Search\" icon=\"icons/search.png\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"CENTER\"/>"
	 * + "</section>" +
	 * "<section text=\"Editor.Store.Section.Stocks\" colspan=\"2\" columns=\"2\" align=\"FILL\" valign=\"FILL\">"
	 * +
	 * "     <table id=\"stocksTable\" fullselection=\"true\" align=\"FILL\" valign=\"FILL\"/>"
	 * + "     <container align=\"BEGINING\" valign=\"FILL\">" +
	 * "         <button id=\"addButton\" text=\"Editor.Store.Button.Add\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"BEGINNING\"/>"
	 * +
	 * "         <button id=\"renameButton\" text=\"Editor.Store.Button.Move\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"BEGINNING\"/>"
	 * +
	 * "         <button id=\"groupButton\" text=\"Editor.Store.Button.Grouping\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"BEGINNING\"/>"
	 * + "     </container>" + "</section>" + "</layout>";
	 */
	
	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);

		form.setText(((DummyEditorInput) getEditorInput()).getName());
		form.setBackgroundImage(Activator.getImage("icons/form_background.png"));

		// createPartControl1(parent);
		Map<String, Control> components = UIFactory.createUI(form.getBody(), layout, toolkit);
		storesCombo = (Combo) components.get("storesCombo");
		stockField = (Text) components.get("filterField");
		// stocksTable = (Table) components.get("stocksTable");
		// salesTable = (Table) components.get("stocksTable");
		// Button addButton = (Button) components.get("addButton");



		// tool-bar buttons
		{
			Action action = new Action("print", Action.AS_PUSH_BUTTON) { //$NON-NLS-1$
				public void run() {
					IWorkbenchPage page = getSite().getPage();
					try {
						ColumnDialog dialog = new ColumnDialog(Application.getActiveWorkbench().getShell(), stocksTable);

						if (dialog.open() == Window.OK) {
							final boolean[] selectedColums = dialog.selectedColums;
							final TableColumn[] columns = stocksTable.getColumns();
							final StringBuilder html = new StringBuilder();

							html.append("<HTML><head><STYLE type=\"text/css\"> "
									+ "body{ background-color: #FFFF; color: #606060; font-size: 13px; font-family: tahoma, verdana, arial, sans-serif; margin: 0; padding: 0; height: 100%; } "
									+ "table { background-color: #FFFFFF; font-size: 11px; }</STYLE></head><BODY>");
							html.append("<table border=1 width='100%' cellspacing=0 cellpadding=1 style='border: #ccc solid 1px;'>");
							html.append("<tr>");
							for (int i = 0; i < selectedColums.length; i++) {
								if (selectedColums[i]) {
									html.append("<td nowrap=true><b>");
									html.append(columns[i].getText());
									html.append("</b></td>");
								}
							}
							html.append("</tr>");
							for (int j = 0; j < stocksTable.getItemCount(); j++) {

								html.append("<tr>");
								for (int i = 0; i < selectedColums.length; i++) {
									if (selectedColums[i]) {
										html.append("<td>");
										html.append(stocksTable.getItem(j).getText(i));
										html.append("&nbsp;</td>");
									}
								}
								html.append("</tr>");
							}
							html.append("</table></BODY></HTML>");

							String name = _("Dialog.StoredStock.Print");
							DummyEditorInput editorInput = new DummyEditorInput(Application.ID.getUID(), name);
							editorInput.setData(html);
							page.openEditor(editorInput, HTMLPrintEditor.ID);
						}
					} catch (PartInitException e) {
						Application.log.log(Level.OFF, "", e);
					}
				}
			};
			action.setToolTipText(_("Editor.Store.Tooltip.Print"));
			action.setImageDescriptor(Activator.getImageDescriptor("icons/print.png"));
			form.getToolBarManager().add(action);
		}
		{
			Action action = new Action("refresh", Action.AS_PUSH_BUTTON) { //$NON-NLS-1$
				public void run() {
					refresh();
				}
			};
			action.setToolTipText(_("Editor.Store.Tooltip.Refresh"));
			action.setImageDescriptor(Activator.getImageDescriptor("icons/refresh.png"));
			form.getToolBarManager().add(action);
		}
		form.updateToolBar();
		refresh();
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void doSave(IProgressMonitor monitor) {}

	@Override
	public void doSaveAs() {}

}
